package com.hll.servlet.user;

import com.hll.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 退出登录，移除session中的用户对象Constants.USER_SESSION
 *
 * @author: Mr.Hu
 * @create: 2021-03-06 20:38
 */
public class LogoutServlet extends HttpServlet {
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    Object attribute = req.getSession().getAttribute(Constants.USER_SESSION);
    if (attribute != null) { // 登录状态
      req.getSession().removeAttribute(Constants.USER_SESSION); // 移出属性
    }
    resp.sendRedirect(req.getContextPath() + "/login.jsp"); // 反正点了注销就要回去
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    doGet(req, resp);
  }
}
